package 每日一题;

import org.junit.Test;
import 二叉树相关问题.中度题.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class 路径总和3 {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    int sum=0;
    public int pathSum(TreeNode root, int targetSum) {
        if (root==null)
            return 0;
        dfs(root,targetSum,0);
        pathSum(root.left,targetSum);
        pathSum(root.right,targetSum);
        return sum;
    }
    private void dfs(TreeNode root, int targetSum ,int num) {
        if (root==null)
            return;
        num+=root.val;
        if (num==targetSum){
            sum++;
        }
        dfs(root.left,targetSum,num);
        dfs(root.right,targetSum,num);
    }
}
